package com.youku
{
   import flash.events.EventDispatcher;
   import flash.utils.Timer;
   import flash.net.URLLoader;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.events.Event;
   import flash.net.URLLoaderDataFormat;
   import flash.events.TimerEvent;
   import flash.net.URLRequest;
   import flash.utils.ByteArray;
   import com.youku.utils.PlayListUtil;
   import flash.display.Loader;
   import flash.system.LoaderContext;
   import flash.system.ApplicationDomain;
   
   public class P2PLoader extends EventDispatcher
   {
      
      public function P2PLoader() {
         super();
      }
      
      public static var COMPLETE:String = "com.youku.p2ploader.complete";
      
      private var _loaderTimer:Timer;
      
      private var _timeout:Number = 30000.0;
      
      private var _urlLoader:URLLoader;
      
      private var _isTimeout:Boolean;
      
      public function load(param1:String) : void {
         var _loc2_:String = null;
         P2PConfig.P2PStatus = P2PConfig.LOAD_FAIL_ERROR;
         this._urlLoader = new URLLoader();
         this._urlLoader.addEventListener(IOErrorEvent.IO_ERROR,this.onURLLoaderError);
         this._urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onURLLoaderError);
         this._urlLoader.addEventListener(Event.COMPLETE,this.onURLLoaderComplete);
         this._urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
         this._loaderTimer = new Timer(this._timeout,1);
         this._loaderTimer.addEventListener(TimerEvent.TIMER_COMPLETE,this.onURLLoaderTimeoutError);
         _loc2_ = "http://cfg.ykpp.navi.youku.com/stream.swf";
         if(PlayerConfig.rootData.ykstreamURL != null)
         {
            _loc2_ = PlayerConfig.rootData.ykstreamURL;
         }
         this._urlLoader.load(new URLRequest(_loc2_));
         this._isTimeout = false;
         this._loaderTimer.reset();
         this._loaderTimer.start();
      }
      
      private function onLoadComplete(param1:Event = null) : void {
         dispatchEvent(new Event(COMPLETE,false,false));
      }
      
      private function onURLLoaderComplete(param1:Event) : void {
         if(this._isTimeout)
         {
            return;
         }
         this._loaderTimer.stop();
         this._loaderTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,this.onURLLoaderTimeoutError);
         this.removeListener();
         var _loc2_:ByteArray = param1.target.data;
         var _loc3_:ByteArray = PlayListUtil.getInstance().decode(_loc2_);
         var _loc4_:Loader = new Loader();
         _loc4_.contentLoaderInfo.addEventListener(Event.COMPLETE,this.libReady);
         _loc4_.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.ioErrorHandler);
         _loc4_.loadBytes(_loc3_,new LoaderContext(false,ApplicationDomain.currentDomain));
      }
      
      private function onURLLoaderError(param1:Event) : void {
         this._loaderTimer.stop();
         this._loaderTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,this.onURLLoaderTimeoutError);
         this.removeListener();
         this.onLoadComplete();
      }
      
      private function onURLLoaderTimeoutError(param1:TimerEvent) : void {
         this._loaderTimer.stop();
         this._loaderTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,this.onURLLoaderTimeoutError);
         this._isTimeout = true;
         this.removeListener();
         this.onLoadComplete();
      }
      
      private function libReady(param1:Event) : void {
         P2PConfig.P2PStatus = P2PConfig.LOAD_LIB_SUCCESS;
         this.onLoadComplete();
      }
      
      private function ioErrorHandler(param1:Event) : void {
         this.onLoadComplete();
      }
      
      private function removeListener() : void {
         this._urlLoader.removeEventListener(IOErrorEvent.IO_ERROR,this.onURLLoaderError);
         this._urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onURLLoaderError);
         this._urlLoader.removeEventListener(Event.COMPLETE,this.onURLLoaderComplete);
      }
   }
}
